package com.agent.generator.controller;

import com.agent.common.result.ApiResponse;
import com.agent.common.utils.UserInfoUtils;
import com.agent.generator.service.GeneratedTaskService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 任务管理控制器
 * @author Jeff_Wan
 * @description 处理AI生成任务的创建与查询
 */
@RestController
@RequestMapping("/tasks")
@RequiredArgsConstructor
public class GeneratedTaskController {
    private final GeneratedTaskService generatedTaskService;

    /**
     * 创建视频生成任务
     *
     * @param model 模型名称
     * @param prompt 提示词
     * @param imageFile 输入图片
     * @param duration 视频时长(秒)
     * @param resolution 分辨率
     * @param aspectRatio 画面比例
     * @return 任务ID
     */
    @PostMapping("/video")
    public ApiResponse<Long> createVideoTask(
            @RequestParam("model") String model,
            @RequestParam(name = "prompt", required = false) String prompt,
            @RequestParam(name = "imageFile", required = false) MultipartFile imageFile,
            @RequestParam("duration")  int duration,
            @RequestParam("resolution")  String resolution,
            @RequestParam("aspectRatio")  String aspectRatio) {

        Long taskId = generatedTaskService.createVideoTask(
                UserInfoUtils.getUserId(), model, prompt, imageFile,
                duration, resolution, aspectRatio
        );
        return ApiResponse.success(taskId);
    }

    /**
     * 创建图片生成任务
     *
     * @param model 模型名称
     * @param prompt 提示词
     * @param imageFile 输入图片
     * @param aspectRatio 画面比例
     * @param quantity 生成数量
     * @return 任务ID
     */
    @PostMapping("/image")
    public ApiResponse<Long> createImageTask(
            @RequestParam("model")  String model,
            @RequestParam(name = "prompt", required = false) String prompt,
            @RequestParam(name = "imageFile", required = false) MultipartFile imageFile,
            @RequestParam("aspectRatio")  String aspectRatio,
            @RequestParam("quantity")  int quantity) {

        Long taskId = generatedTaskService.createImageTask(
                UserInfoUtils.getUserId(), model, prompt, imageFile,
                aspectRatio, quantity
        );
        return ApiResponse.success(taskId);
    }

    /**
     * 获取任务状态
     * @param taskId 任务ID
     * @return 当前状态
     */
    @GetMapping("/status")
    public ApiResponse<String> getTaskStatus(@RequestParam("taskId") Long taskId) {
        String status = generatedTaskService.getTaskStatus(taskId);
        return ApiResponse.success(status);
    }

    /**
     * 获取任务详情
     * @param taskId 任务ID
     * @return 完整任务数据
     */
    @GetMapping("/info")
    public ApiResponse<Object> getTaskDetails(@RequestParam("taskId") Long taskId) {
        Object taskDetails = generatedTaskService.getTaskDetails(taskId);
        return ApiResponse.success(taskDetails);
    }
}